package com.vincce.sport.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.LinkedList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import com.vincce.sport.core.bbs.client.Client;
import com.vincce.sport.core.bbs.util.XMLHelper;

@Controller
@RequestMapping("/bbs")
public class BbsController extends IndexController{
	/**
	 * @param args
	 */
	public static void main(String[] args) {

		//testLogin();
		Client uc = new Client();
		String s = uc.uc_authcode("8485m7QEfsvnOg9tKGvAsxlwXpAzZU6LhFA04pD6N0XIYG1cJVDh2Th83Qcci130UPHbXp+UNG0","DECODE");
		System.out.println(s);
	
	}
	
	@RequestMapping("/login")
	public String login(Model model,HttpServletRequest request,HttpServletResponse response,String password,String userName,String href) throws ParseException{
		try {
			testLogin(response,userName,password,href);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}
	
	@RequestMapping("/register")
	public String register(Model model){
//		testRegister();
		return null;
	}
	
	public static void testLogin(HttpServletResponse response,String userName,String password,String href) throws IOException{
//		Boolean flag = false;
		response.setContentType( "text/html;charset=utf-8" ) ;  //设置响应页面字符编码
        PrintWriter out = response.getWriter() ;
        String js = "";
		Client e = new Client();
		String result = e.uc_user_login(userName, password);
		LinkedList<String> rs = XMLHelper.uc_unserialize(result);
		if(rs.size()>0){
			int $uid = Integer.parseInt(rs.get(0));
			String $username = rs.get(1);
			String $password = rs.get(2);
			String $email = rs.get(3);
			if($uid > 0) {
				response.addHeader("P3P"," CP=\"CURa ADMa DEVa PSAo PSDo OUR BUS UNI PUR INT DEM STA PRE COM NAV OTC NOI DSP COR\"");
				
				System.out.println("登录成功");
				System.out.println($username);
				System.out.println($password);
				System.out.println($email);
				
				String $ucsynlogin = e.uc_user_synlogin($uid);
				System.out.println("登录成功"+$ucsynlogin);

				js = $ucsynlogin;
//		        out.println("<style type=\"text/css\">body {background-image: url(\"/admin/site/common/img/bggradient.jpg\");background-repeat: repeat-x;background-size: 1px 100%;height:100%;}</style>");
//		        out.println("<div style=\"font-size:30px;line-height:100px;text-align:center; font-weight: bold; line-height: 600px;\" >登陆中...</div>"+$ucsynlogin);
//		        out.println("<script>window.location.href='"+href+"'</script>");
//		        flag = true;
			} else if($uid == -1) {
				System.out.println("用户不存在,或者被删除");
				String email=System.currentTimeMillis()+"@163.com";
				String msg = testRegister($username, $password, email);
				if(msg.equals("OK")){
					testLogin(response, $username, $password,href);
				}
			} else if($uid == -2) {
				System.out.println("密码错");
			} else {
				System.out.println("未定义");
			}
		}else{
			System.out.println("Login failed");
			System.out.println(result);
		}
		out.println("<style type=\"text/css\">body {background-image: url(\"/admin/site/common/img/bggradient.jpg\");background-repeat: repeat-x;background-size: 1px 100%;height:100%;}</style>");
		out.println("<div style=\"font-size:30px;line-height:100px;text-align:center; font-weight: bold; line-height: 600px;\" >登陆中...</div>"+js);
		out.println("<script>window.location.href='"+href+"'</script>");
		out.close() ;
	}
	
	public static void testLogout(HttpServletResponse response){
		
		Client uc = new Client();

//		生成同步退出的代码
		String $ucsynlogout = uc.uc_user_synlogout();
		System.out.println("退出成功"+$ucsynlogout);
		
		try {
			response.setContentType( "text/html;charset=utf-8" ) ;  //设置响应页面字符编码
			PrintWriter out = response.getWriter();
			out.println("<style type=\"text/css\">body {background-image: url(\"/admin/site/common/img/bggradient.jpg\");background-repeat: repeat-x;background-size: 1px 100%;height:100%;}</style>");
	        out.println("<div style=\"font-size:30px;line-height:100px;text-align:center; font-weight: bold; line-height: 600px;\" >退出中。。。</div>"+$ucsynlogout);
	        out.println("<script>window.location.href='/index.html'</script>");
	        out.close() ;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public static String testRegister(String userName,String password,String email){
		String msg = "";
		Client uc = new Client();

//		生成同步退出的代码
		String $returns = uc.uc_user_register(userName, password, email);
		int $uid = Integer.parseInt($returns);
		if($uid <= 0) {
			if($uid == -1) {
				System.out.print("用户名不合法");
				msg = "用户名不合法";
			} else if($uid == -2) {
				System.out.print("包含要允许注册的词语");
				msg = "包含要允许注册的词语";
			} else if($uid == -3) {
				System.out.print("用户名已经存在");
				msg = "用户名已经存在";
			} else if($uid == -4) {
				System.out.print("Email 格式有误");
				msg = "Email 格式有误";
			} else if($uid == -5) {
				System.out.print("Email 不允许注册");
				msg = "Email 不允许注册";
			} else if($uid == -6) {
				System.out.print("该 Email 已经被注册");
				msg = "该 Email 已经被注册";
			} else {
				System.out.print("未定义");
				msg = "未定义";
			}
		} else {
			System.out.println("OK:"+$returns);
			msg = "OK";
		}
		return msg;
	}
}
